package com.identify.identifydrawings;

import android.os.Environment;
import android.util.Log;

import com.identify.identifydrawings.netUtils.Url;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ListIterator;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ImageUploadAdress {

    /**
     * The imgur client ID for OkHttp recipes. If you're using imgur for anything other than running
     * these examples, please request your own client ID! https://api.imgur.com/oauth2
     */
    private static final String IMGUR_CLIENT_ID = "123";
    private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");
    private static final OkHttpClient client = new OkHttpClient();
    //原是static
    public static void run(String s , final ResponseListener responseListener) throws Exception {
        Log.e("TAG","run="+s);
        final String str=s;
        new Thread() {
          @Override
            public void run() {
//              final Request request = new Request.Builder()
//                      .get()
//                      .url("http://192.168.137.1:5000/uploadcutimage" + "?path=" + file)
//                      .build();
              Request request = new Request.Builder()
                      .url("http://192.168.137.1:5000/uploadcutimage")
                      .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, str))
                      .build();
              //修改的代码
              Call call = client.newCall(request);
              call.enqueue(new Callback() {
                  @Override
                  public void onFailure(Call call, IOException e) {
                      Log.e("TAG", "onFailure: " + e.getMessage());

                  }

                  @Override
                  public void onResponse(Call call, Response response) throws IOException {
                      //  String dirName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LungFile";
                      Log.e("TAG", "response=" + response);
                      //文件夹下有图片
                      String dirName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/dddd";

                      final File file = new File(dirName);
                      if (!file.exists()) {
                          file.mkdir();
                      }
// UUID.randomUUID().toString()
                      File file1 = new File(dirName + "/" +UUID.randomUUID().toString()+".png");

                      if (response != null) {
                          InputStream is = response.body().byteStream();
                          FileOutputStream fos = new FileOutputStream(file1);
                          int len = 0;
                          byte[] buffer = new byte[2048];
                          while (-1 != (len = is.read(buffer))) {
                              fos.write(buffer, 0, len);
                          }
                          fos.flush();
                          fos.close();
                          is.close();
                      }
                      responseListener.onResonse(file1.getAbsolutePath());
                  }
              });
          }
              }.start();
    }
}


